namespace NServiceBus;

using System.Threading.Tasks;
using Pipeline;
using Transport;
using Unicast.Messages;

class NativeUnsubscribeTerminator : PipelineTerminator<IUnsubscribeContext>
{
    public NativeUnsubscribeTerminator(ISubscriptionManager subscriptionManager, MessageMetadataRegistry messageMetadataRegistry)
    {
        this.subscriptionManager = subscriptionManager;
        this.messageMetadataRegistry = messageMetadataRegistry;
    }

    protected override Task Terminate(IUnsubscribeContext context)
    {
        var eventMetadata = messageMetadataRegistry.GetMessageMetadata(context.EventType);
        return subscriptionManager.Unsubscribe(eventMetadata, context.Extensions, context.CancellationToken);
    }

    readonly ISubscriptionManager subscriptionManager;
    readonly MessageMetadataRegistry messageMetadataRegistry;
}